<script setup lang="ts">
import { message } from 'ant-design-vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost
} from '@/servers/api/pictureController.ts'
import { ref, reactive, onMounted, computed } from 'vue'
import PictureCard from '@/components/picture-card/index.vue'

// 数据
const dataList = ref([])
const total = ref(0)
const loading = ref(true)

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend'
})

const categoryList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
const selectedTagList = ref<string[]>([])

// 获取标签和分类选项
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.code == 200 && res.data) {
    // 转换成下拉选项组件接受的格式
    categoryList.value = res.data.categoryList ?? []
    tagList.value = res.data.tagList ?? []
  }
}

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current ?? 1,
    pageSize: searchParams.pageSize ?? 10,
    total: total.value,
    // 切换页号时，会修改搜索参数并获取数据
    onChange: (page, pageSize) => {
      searchParams.current = page
      searchParams.pageSize = pageSize
      fetchData()
    }
  }
})

// 获取数据
const fetchData = async () => {
  loading.value = true
  // 转换搜索参数
  const params = {
    ...searchParams,
    tags: [],
  }
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  const res = await listPictureVoByPageUsingPost(params)
  if (res.data) {
    dataList.value = res.data.records ?? []
    total.value = res.data.total ?? 0
  }
  loading.value = false
}


const doSearch = () => {
  // 重置搜索条件
  searchParams.current = 1
  fetchData()
}

// 页面加载时请求一次
onMounted(() => {
  fetchData()
  getTagCategoryOptions()
})
</script>

<template>
  <div class="home">
    <!-- 头部 -->
    <div class="head">
      <!-- 搜索框 -->
      <div class="search-bar">
        <a-input-search
          placeholder="从海量图片中搜索"
          v-model:value="searchParams.searchText"
          enter-button="搜索"
          size="large"
          @search="doSearch"
        />
      </div>
      <!-- 分类 + 标签 -->
      <a-tabs v-model:activeKey="selectedCategory" @change="doSearch">
        <a-tab-pane key="all" tab="全部" />
        <a-tab-pane v-for="category in categoryList" :key="category" :tab="category" />
      </a-tabs>
      <div class="tag-bar">
        <span style="margin-right: 8px">标签：</span>
        <a-space :size="[0, 8]" wrap>
          <a-checkable-tag
            v-for="(tag, index) in tagList"
            :key="tag"
            v-model:checked="selectedTagList[index]"
            @change="doSearch"
          >
            {{ tag }}
          </a-checkable-tag>
        </a-space>
      </div>
    </div>

    <div class="body">
      <!-- 图片列表 -->
      <a-row :gutter="[16, 16]">
        <a-col :span="6" v-for="picture in dataList" :key="picture.id">
          <PictureCard :picture="picture" />
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.home {
  height: 100%;
}

.head {
  height: 160px;
  //background: url("@/assets/images/system/home-banner.jpg");
  //background-size: cover;
  max-width: 1200px;
  margin: 0 auto;

  .search-bar {
    max-width: 480px;
    margin: 0 auto 16px;
  }

}

.body {
  max-width: 1200px;
  margin: 0 auto;
}
</style>
